package com.yj.model.comm;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.json.JSONObject;

/**
 * @author sunweiwei
 */
public class ResultUtil {
    /**
     * 日志
     */
    private static Logger logger = LoggerFactory.getLogger(ResultUtil.class);
    //保存json对象
    private Map<String, Object> results;
    //消息"key
    public static final String MSG = "msg";
    //成功标识 key
    public static final String SUCCESS = "success";
    //单对"key
    public static final String OBJ = "obj";
    //列表对象 key
    public static final String ROWS = "rows";
    //总计"key
    public static final String TOTAL = "total";
    //状" key
    public static final String STATUS = "status";

    public ResultUtil() {
        this.results = new HashMap<>();
        this.results.put(SUCCESS, true);
    }

    public Map<String, Object> getResult() {
        return this.results;
    }

    public void setResult(Map<String, Object> set) {
        this.results = set;
    }

    public boolean getSuccess() {
        return (Boolean) results.get(SUCCESS);
    }

    public String getMessage() {
        return (String) results.get(MSG);
    }

    public void setRows(Object list) {
        this.results.put(ROWS, list);
    }

    public void setTotal(Integer total) {
        this.results.put(TOTAL, total);
    }

    public String getStatus() {
        return (String) results.get(STATUS);
    }

    /**
     * 根据"  自定义添加属性"
     *
     * @param key   属性标识
     * @param value
     */
    public void setProperty(String key, Object value) {
        try {
            this.results.put(key, value);
        } catch (RuntimeException e) {
            logger.error("-->>设置key为：" + key + "值为" + value + " Json时出错：", e);
        }
    }

    /**
     * 设置状态信息
     *
     * @param status
     */
    public void setStatus(String status) {
        setProperty(STATUS, status);
    }

    /**
     * 设置成功标志,用于程序执行是否正常
     *
     * @param success 成功标识
     */
    public void setSuccess(boolean success) {
        setProperty(SUCCESS, success);
    }

    /**
     * 设置消息
     *
     * @param msg 消息
     */
    public void setMsg(String msg) {
        setProperty(MSG, msg);
    }

    /**
     * 设置总计
     *
     * @param size
     */
    public void setTotal(int size) {
        setProperty(TOTAL, size);
    }

    /**
     * 添加对象列表 数据为json格式
     *
     * @param data 对象列表json格式
     */
    public void setData(String data) {
        setProperty(ROWS, data);
    }

    /**
     * 添加对象
     *
     * @param obj 对象
     */
    public void setObject(Object obj) {
        setProperty(OBJ, obj);
    }

    /**
     * 返回json格式
     */
    public String toJSONString() {
        JSONObject obj = new JSONObject();
        obj.put("data", this.results);
        return obj.toString();
    }

    public static void main(String aregs[]) {
        ResultUtil util = new ResultUtil();
        System.out.println(util.toString());
    }

}
